package com.example.memolaga.credentials;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Activity;
import android.preference.PreferenceManager;
import android.text.InputType;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.memolaga.Constants;
import com.example.memolaga.Functions;
import com.example.memolaga.R;
import com.example.memolaga.password.FunctionsPass;

public class CredEdit extends Activity {

    private String sKey;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cred_edit);
        String credName="";
        Bundle bundle = this.getIntent().getExtras();
        if (bundle!=null)
        {
            this.sKey = bundle.getString(Constants.getKeyString());
            credName = bundle.getString(Constants.getCardsName());
        }
        else
            Functions.showLongToast(getApplicationContext(), getString(R.string.str_error_loading_inf));

        if (!credName.equals(""))
        {
            CredentialMaster cred = CredentialManager.getInstance().getCredential(credName);
            ((TextView) findViewById(R.id.txtCredNameViewEdit)).setText(cred.getName());
            ((EditText) findViewById(R.id.editCredentialUserEdit)).setText(FunctionsPass.decryptString(this.sKey, cred.getUser()));
            ((EditText) findViewById(R.id.editCredentialPassEdit)).setText(FunctionsPass.decryptString(this.sKey, cred.getPass()));
            ((EditText) findViewById(R.id.editCredentialDateCreatedEdit)).setText(cred.getDateCreated());
            ((EditText) findViewById(R.id.editCredentialDateLastUsedEdit)).setText(cred.getDateLastUsed());
            ((EditText) findViewById(R.id.editCredentialExpiryEdit)).setText(cred.getDataExpiry());
            ((EditText) findViewById(R.id.editCredentialSiteEdit)).setText(cred.getSite());
            ((EditText) findViewById(R.id.editCredentialNoteEdit)).setText(cred.getNote());
            ((Button) findViewById(R.id.btnCredSaveEdit)).setOnClickListener(editCredListener);
            SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
            if (preferences.getBoolean(Constants.getPrefStringShowPass(), Constants.getPrefDefaultShowPass()))
            {
                ((ImageView) findViewById(R.id.imgViewCardCvvView)).setVisibility(View.GONE);
                ((TextView) findViewById(R.id.editCredentialUserEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                ((ImageView) findViewById(R.id.imgViewCardNumberView)).setVisibility(View.GONE);
                ((TextView) findViewById(R.id.editCredentialPassEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
            }
            ((ImageView) findViewById(R.id.imgViewCardCvvView)).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view) {
                    if (((EditText) findViewById(R.id.editCredentialUserEdit)).getInputType()!= InputType.TYPE_CLASS_NUMBER)
                    {
                        ((ImageView) findViewById(R.id.imgViewCardCvvView)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                        ((EditText) findViewById(R.id.editCredentialUserEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                    }
                    else
                    {
                        ((ImageView) findViewById(R.id.imgViewCardCvvView)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                        ((EditText) findViewById(R.id.editCredentialUserEdit)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                    }
                }
            });
            ((ImageView) findViewById(R.id.imgViewCardNumberView)).setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view) {
                    if (((EditText) findViewById(R.id.editCredentialPassEdit)).getInputType()!= InputType.TYPE_CLASS_NUMBER)
                    {
                        ((ImageView) findViewById(R.id.imgViewCardNumberView)).setImageDrawable(getResources().getDrawable(R.drawable.secure2));
                        ((EditText) findViewById(R.id.editCredentialPassEdit)).setInputType(InputType.TYPE_CLASS_NUMBER);
                    }
                    else
                    {
                        ((ImageView) findViewById(R.id.imgViewCardNumberView)).setImageDrawable(getResources().getDrawable(R.drawable.not_secure2));
                        ((EditText) findViewById(R.id.editCredentialPassEdit)).setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_VARIATION_PASSWORD);
                    }
                }
            });
        }
    }

    private View.OnClickListener editCredListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = ((TextView) findViewById(R.id.txtCredNameViewEdit)).getText().toString(), user = ((EditText) findViewById(R.id.editCredentialUserEdit)).getText().toString();
            String pwd = ((EditText) findViewById(R.id.editCredentialPassEdit)).getText().toString(), dateCreated=  ((EditText) findViewById(R.id.editCredentialDateCreatedEdit)).getText().toString();
            String dateLastUsed = ((EditText) findViewById(R.id.editCredentialDateLastUsedEdit)).getText().toString(), expiry = ((EditText) findViewById(R.id.editCredentialExpiryEdit)).getText().toString();
            String site = ((EditText) findViewById(R.id.editCredentialSiteEdit)).getText().toString(), note = ((EditText) findViewById(R.id.editCredentialNoteEdit)).getText().toString();
            if (CredentialManager.getInstance().modCredential(name, user, pwd, dateCreated, dateLastUsed,
                            expiry, site, note, sKey))
            {
                Functions.showLongToast(getBaseContext(), getString(R.string.strOKCreateFile));
                setResult(Constants.getResultEdit());
                finish();
            }
            else
                Functions.showLongToast(getBaseContext(), getString(R.string.strErrorCreateFile));
        }
    };

/*
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.cred_edit, menu);
        return true;
    }*/
    
}
